package com.zeedev.flyingsaucer.beers.activities.storechooser;

import android.content.Context;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.zeedev.flyingsaucer.beers.downloaders.StoreDownloader;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv;
import roboguice.util.Ln;

/**
 * User: James
 * Date: 1/2/11
 * Time: 10:23 PM
 */
public class StoreSpinner extends Spinner {

    public StoreSpinner(Context context) {
        super(context);
    }

    public StoreSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public StoreSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    @Inject
    Provider<StoreDownloader> downloaderProvider;

    @Inject
    public void setAdapter(SimpleCursorAdapter adapter) {
        super.setAdapter(adapter);

        if (getAdapter().getCount() < 1) {


            downloaderProvider.get().execute();

/*
            try {
                downloaderProvider.get().call();
            } catch (Exception e) {
                e.printStackTrace();
                Ln.e(e,"Storesdownloader error.");
            }
*/


        }

    }

    String storeNumber = null;

    public String getStoreNumber() {
//        Cursor wrapper = (Cursor) getSelectedItem();
//        Integer num = wrapper.getInt(wrapper.getColumnIndex(FlyingSaucerConv.Stores.NUMBER));
//        String number = wrapper.getString(wrapper.getColumnIndex(FlyingSaucerConv.Stores.NUMBER));
//        if (number != null) {
//            return number.toString();
//        } else {
//            return null;
//        }
        return storeNumber;
    }

    public void changeSpinner(String storeNumber) {
        if (TextUtils.isEmpty(storeNumber)) {
            Ln.w("Setting spinner to null.");
            storeNumber = null;
            return;
        } else {
            this.storeNumber = storeNumber;
        }
        Integer storePosition = findStorePositionFromStoreNumber(storeNumber);
        if (storePosition != null) {
            setSelection(storePosition, true);
            Ln.d("Store in spinner set to: " + storePosition);
        } else {
            Ln.w("Store in spinner could not be selected.");
        }
    }

    public void setStoreNumberWeak(String storeNumber) {
        this.storeNumber = storeNumber;
    }

    private Integer findStorePositionFromStoreNumber(String storeNumber) {
        Integer ret = null;
        for (int position = 0; position < getAdapter().getCount(); position++) {
            Cursor wrapper = (CursorWrapper) getAdapter().getItem(position);
            Integer number = wrapper.getInt(wrapper.getColumnIndex(FlyingSaucerConv.Stores.NUMBER));
            if (number != null) {

                if (number.toString().equals(storeNumber)) {

                    ret = position;
                    break;

                }
            }
        }
        return ret;
    }

    public void refreshStoresList() {
        downloaderProvider.get().execute();
    }

    public Boolean isRefreshing() {
        return !isEnabled();
    }


    public void setRefreshing(Boolean refreshing) {
        this.setEnabled(!refreshing);
    }
}
